package com.boot.security.server.controller.system;

import java.util.List;

import com.boot.security.server.model.mffl.Vo.MfflRefundVo;
import com.boot.security.server.service.impl.mffl.MfflRefund.IMfflRefund;
import com.boot.security.server.utils.mffl.ReturnBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.boot.security.server.page.table.PageTableRequest;
import com.boot.security.server.page.table.PageTableHandler;
import com.boot.security.server.page.table.PageTableResponse;
import com.boot.security.server.page.table.PageTableHandler.CountHandler;
import com.boot.security.server.page.table.PageTableHandler.ListHandler;
import com.boot.security.server.dao.mffl.MfflRefundDao;
import com.boot.security.server.model.mffl.MfflRefund;

import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

//@ApiIgnore
@RestController
@RequestMapping("/mfflRefunds")
public class MfflRefundController {

    @Autowired
    private MfflRefundDao mfflRefundDao;
    @Autowired
    private IMfflRefund mfflRefund;

    private Long id;

    @PostMapping
    @ApiOperation(value = "保存")
    public MfflRefund save(@RequestBody MfflRefund mfflRefund) {
        mfflRefundDao.save(mfflRefund);

        return mfflRefund;
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id获取")
    public MfflRefund get(@PathVariable Long id) {
        return mfflRefundDao.getById(id);
    }

    @PutMapping
    @ApiOperation(value = "修改")
    public MfflRefund update(@RequestBody MfflRefund mfflRefund) {
        mfflRefundDao.update(mfflRefund);

        return mfflRefund;
    }

    @GetMapping
    @ApiOperation(value = "列表")
    public PageTableResponse list(PageTableRequest request) {
        return new PageTableHandler(new CountHandler() {

            @Override
            public int count(PageTableRequest request) {
                return mfflRefundDao.count(request.getParams());
            }
        }, new ListHandler() {

            @Override
            public List<MfflRefundVo> list(PageTableRequest request) {
                return mfflRefundDao.list(request.getParams(), request.getOffset(), request.getLimit());
            }
        }).handle(request);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public void delete(@PathVariable Long id) {
        mfflRefundDao.delete(id);
    }


    @PostMapping("/yseRefund/{id}")
    @ApiOperation(value = "同意退款")
    public ReturnBean yseRefund(@PathVariable Long id) {
        MfflRefund mfflRefund = this.mfflRefundDao.getById(id);
        int status = this.mfflRefund.yesRefund(mfflRefund);
        if (status == 0) {
            return new ReturnBean("201", "处理失败");
        }
        return new ReturnBean();
    }

    @PostMapping("/noRefund/{id}")
    @ApiOperation(value = "拒绝退款")
    public ReturnBean noRefund(@PathVariable Long id) {
        MfflRefund mfflRefund = this.mfflRefundDao.getById(id);
        try {
            this.mfflRefund.noRefund(mfflRefund);
        } catch (Exception e) {
            return new ReturnBean("201", "处理失败");
        }

        return new ReturnBean();
    }

}
